<template>
  <div>
    <el-card class="tree-card">

      <tree-tools :obj="companyInfo" :is-root="true" @add-depts="onAdd" />

      <el-tree :data="treeData" :props="defaultProps" :default-expand-all="true">
        <tree-tools
          slot-scope="{data}"
          :obj="data"
          @add-depts="onAdd"
          @edit-depts="onEdit"
        />
      </el-tree>
    </el-card>
    <add-dept
      ref="depts"
      v-model="dialogVisible"
      :node="treeNode"
      @add-success="loadDepartments"
      @edit-success="loadDepartments"
    />
  </div>
</template>

<script>
import treeTools from './componments/tree-tools.vue'
import { getDepartments } from '@/api/departments.js'
import { transListToTree } from '@/utils/index.js'
import AddDept from './componments/add-dept.vue'
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      companyInfo: {
        name: '传智播客',
        manager: '负责人',
        id: ''
      },
      dialogVisible: false,
      treeNode: {}
    }
  },

  created() {
    this.loadDepartments()
  },

  methods: {
    async loadDepartments() {
      // 获取部门信息列表
      const res = await getDepartments()
      console.log(res)
      this.treeData = transListToTree(res.depts, '')
    },
    // 添加部门
    onAdd(data) {
      this.dialogVisible = true
      console.log(data)
      this.treeNode = data
    },
    // 编辑部门
    onEdit(data) {
      this.dialogVisible = true
      this.treeNode = data
      this.$refs.depts.getDeptsInfo(data.id)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
